#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

int main()
{
    string str = "MCMXCIV";

    // 使用map来优化存储
    map<char, int> stand = {
        {'I', 1},
        {'V', 5},
        {'X', 10},
        {'L', 50},
        {'C', 100},
        {'D', 500},
        {'M', 1000}};

    // 检查输入字符串是否有效
    for (char c : str)
    {
        if (stand.find(c) == stand.end())
        {
            cerr << "错误: 字符 '" << c << "' 不在允许的罗马数字范围内。" << endl;
            return 1; // 返回错误代码
        }
    }

    int len = str.size();
    int sumdata = 0;
    vector<char> s;
    // cout << "字符长度为：" << len << endl;

    // 进一步处理字符串可以在此处进行，例如转换成对应的整数
    vector<int> sum;

    for (int i = 0; i < len; i++)
    {
        s.push_back(str[i]);
        sum.push_back(stand[s[i]]);
    }

    for (int i = 0; i < len - 1; i++)
    {
        if (s[i] == 'I' && (s[i + 1] == 'V' || s[i + 1] == 'X'))
            sum[i] = -sum[i];

        if (s[i] == 'X' && (s[i + 1] == 'L' || s[i + 1] == 'C'))
            sum[i] = -sum[i];

        if (s[i] == 'C' && (s[i + 1] == 'D' || s[i + 1] == 'M'))
            sum[i] = -sum[i];
    }

    for (int i = 0; i < len; i++)
    {
        sumdata = sumdata + sum[i];
    }

    cout << str << "对应的值为：" << sumdata << endl;
}
